package org.fall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.entity.Role;
import org.entity.RoleExample;
import org.fall.mapper.RoleMapper;
import org.fall.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
@Autowired
private RoleMapper roleMapper;

    @Override
    public PageInfo<Role> getPageInfo(Integer pageNum, Integer pageSize, String keyword) {
        // 开启分页
        PageHelper.startPage(pageNum, pageSize);

        // 从mapper方法得到Role的List
        List<Role> roles = roleMapper.selectRoleByKeyword(keyword);

        // 封装为PageInfo对象
        PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
        // 返回pageInfo
        return pageInfo;
    }

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void deleteByArray(List<Integer> arrayStr) {
        // 获取Criteria对象
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        // 使用Criteria封装查询条件
        criteria.andIdIn(arrayStr);
        roleMapper.deleteByExample(roleExample);
    }

    @Override
    public List<Role> getAssignedRole(Integer adminId) {
        return roleMapper.queryAssignedRoleList(adminId);
    }

    @Override
    public List<Role> getUnAssignedRole(Integer adminId) {
        return roleMapper.queryUnAssignedRoleList(adminId);
    }
}
